package com.zhongketech.sunflower.transport.im;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import com.zhongketech.sunflower.MyToast;
import com.zhongketech.sunflower.transport.LogUtil;

public class DataPackage {

	private static final String TAG = "DataPackage";

	/*
	 * MSG 定义字段
	 * */
	public static final int MSG_LENGHT_START = 0;
	public static final int MSG_LENGHT_SIZE = 2;
	public static final int MSG_SEQ_START = 2;
	public static final int MSG_SEQ_SIZE = 2;
	public static final int MSG_COMMAND_START = 4;
	public static final int MSG_COMMAND_SIZE = 2;
	public static final int MSG_BODY_START = 6;

	public static byte[] composeMsg(final short seq,final short command,final Map<String, String> bodyParams)
	{
		//must check body lenght,max == short lenght

		
		short lenght = MSG_SEQ_SIZE + MSG_COMMAND_SIZE;
		String body = null;
		if(null != bodyParams)
		{
			body = "{";
			Set set = bodyParams.entrySet();
			Iterator it = set.iterator();
			Map.Entry mapEntry = (Map.Entry) it.next();
			body = body+"\""+mapEntry.getKey()+"\""+":"+"\""+mapEntry.getValue()+"\"";
			while (it.hasNext()) { 
				mapEntry = (Map.Entry) it.next();
				body = body+","+"\""+mapEntry.getKey()+"\""+":"+"\""+mapEntry.getValue()+"\"";
			}
			body = body+"}";
			lenght = (short) (lenght + body.length());	
		}
		
		LogUtil.i(TAG,"composeMsg body:"+body);
		byte[] msg = new byte[MSG_LENGHT_SIZE + lenght];
		byte[] lenghtPackage = shortToByte(lenght);
		byte[] seqPackage = shortToByte(seq);
		byte[] cmPackage = shortToByte(command);

		for(int i = 0;i < MSG_LENGHT_SIZE;i ++)
		{
			msg[i] = lenghtPackage[i];
		}

		for(int i = 0;i < MSG_SEQ_SIZE;i ++)
		{
			msg[MSG_LENGHT_SIZE+i] = seqPackage[i];
		}

		for(int i = 0;i < MSG_COMMAND_SIZE;i ++)
		{
			msg[MSG_LENGHT_SIZE+MSG_SEQ_SIZE+i] = cmPackage[i];
		}

//		if(LogUtil.LogON)
//		{
//			for(int i = 0; i < MSG_BODY_START;i ++)
//			{
//				LogUtil.i(TAG,"composeMsg msg["+i+"] "+byteToHex(msg[i]));
//			}
//
//		}

		if(null != body)
		{
			for(int i = 0;i < body.length();i ++)
			{
				msg[MSG_BODY_START+i] = body.getBytes()[i];
			}
		}

		return msg;
	}


	private static byte[] shortToByte(short x) 
	{


//		LogUtil.i(TAG,"0:"+byteToHex((byte)(x & 0xff)));
//		LogUtil.i(TAG,"1:"+byteToHex((byte)((x >> 8) & 0xff)));

		byte[] result = new byte[2];
		result[1] = (byte)Math.abs((x & 0xff));
		result[0] = (byte)Math.abs(((x >> 8) & 0xff));

//		LogUtil.i(TAG,"result 0:"+byteToHex(result[0]));
//		LogUtil.i(TAG,"result 1:"+byteToHex(result[1]));

		//		LogUtil.i(TAG,"result:"+result);
		return result;
	}

	private static short byteToShort(byte[] x,int index) 
	{
		if(index >= x.length)
		{
			LogUtil.e(TAG,"byteToShort inde to max");
			return 0;
		}

		LogUtil.i(TAG,"0:"+byteToHex(x[index+0]));
		LogUtil.i(TAG,"1:"+byteToHex(x[index+1]));
		byte[] y = new byte[2];
		y[0] = (byte)Math.abs(x[1+index]);
		y[1] = (byte)Math.abs(x[0+index]);
		short result = (short) (((y[1] << 8) | y[0] & 0xff));
//        short s0 = (short) (y[0] & 0xff);// 最低位  
//        short s1 = (short) (y[1] & 0xff);  
//        s1 <<= 8;  
//        short result = (short) (s0 | s1);  
        
//		LogUtil.i(TAG,"result:"+result);

		return result;
	}

	private static String byteToHex(byte b)
	{
		return "0X"+Integer.toHexString(b & 0xFF); 
	}

	public static MsgDao analysisMsg(byte[] msg)
	{
		final short lenght = byteToShort(msg,0);
		final short seq = byteToShort(msg,MSG_LENGHT_SIZE);
		final short command = byteToShort(msg,MSG_LENGHT_SIZE+MSG_SEQ_SIZE);
		final short bodyLenght = (short) (msg.length - (MSG_LENGHT_SIZE+MSG_SEQ_SIZE+MSG_COMMAND_SIZE));
		final byte[] bodyByte = new byte[bodyLenght];
		String body = null;

		if(bodyLenght > 0)
		{
			for(int i = 0;i < bodyLenght;i ++)
			{
				bodyByte[i] = msg[MSG_LENGHT_SIZE+MSG_SEQ_SIZE+MSG_COMMAND_SIZE+i];
			}

			body = new String(bodyByte);	
		}

		MsgDao msgDao = new MsgDao(lenght, seq, command, body);
		if(null != msgDao)
		{
			MyToast.getInstance().show(msgDao.getBody());
		}

		return msgDao;
	}
}
